package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.mapper.AdminOrderMapper;
import com.sky.mapper.UserOrderDetailMapper;
import com.sky.mapper.UserOrderMapper;
import com.sky.result.PageResult;
import com.sky.service.AdminOrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdminOrderServiceImpl implements AdminOrderService {

    @Autowired
    private AdminOrderMapper adminOrderMapper;
    @Autowired
    private UserOrderMapper userOrderMapper;
    @Autowired
    private UserOrderDetailMapper userOrderDetailMapper;

    @Override
    public PageResult conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        PageHelper.startPage(ordersPageQueryDTO.getPage(), ordersPageQueryDTO.getPageSize());

        Page<Orders> orderVOPage = adminOrderMapper.conditionSearch(ordersPageQueryDTO);
        List<OrderVO> orderVOList = orderVOPage.getResult().stream().map(item -> {
            OrderVO orderVO = new OrderVO();
            BeanUtils.copyProperties(item, orderVO);

            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(item.getId());
            ArrayList<OrderDetail> orderDetailArrayList = userOrderDetailMapper.list(orderDetail);
            StringBuilder orderDetails = new StringBuilder();
            for (OrderDetail it : orderDetailArrayList) {
                orderDetails.append(it.getName());
                if (it.getDishFlavor() != null && !it.getDishFlavor().isEmpty()) {
                    orderDetails.append("(").append(it.getDishFlavor()).append(")");
                }
                orderDetails.append(" x").append(it.getNumber()).append("，");
            }
            orderVO.setOrderDishes(orderDetails.substring(0, orderDetails.length() - 1));

            return orderVO;
        }).collect(Collectors.toList());

        return PageResult.success(orderVOPage.getTotal(), orderVOList);
    }

    @Override
    public String delivery(Long id) {
        Orders orders = new Orders();
        orders.setId(id);
        orders.setStatus(Orders.DELIVERY_IN_PROGRESS);


        userOrderMapper.update(orders);
        return null;
    }

    @Override
    public OrderVO details(Long id) {
        Orders orders = userOrderMapper.getById(id);

        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(id);
        List<OrderDetail> orderDetailArrayList = userOrderDetailMapper.list(orderDetail);

        OrderVO orderVO = new OrderVO();
        BeanUtils.copyProperties(orders, orderVO);
        orderVO.setOrderDetailList(orderDetailArrayList);

        return orderVO;
    }

    @Override
    public String confirm(Orders orders) {
        orders.setStatus(Orders.CONFIRMED);

        userOrderMapper.update(orders);
        return null;
    }

    @Override
    public String rejection(Orders orders) {
        orders.setStatus(Orders.CANCELLED);
        orders.setPayStatus(Orders.REFUND);
        orders.setCancelTime(LocalDateTime.now());

        userOrderMapper.update(orders);
        return null;
    }

    @Override
    public String complete(Long id) {
        Orders orders = new Orders();
        orders.setId(id);
        orders.setStatus(Orders.COMPLETED);
        orders.setDeliveryTime(LocalDateTime.now());

        userOrderMapper.update(orders);
        return null;
    }

    @Override
    public OrderStatisticsVO statistics() {
        OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO();
        orderStatisticsVO.setConfirmed(adminOrderMapper.getConfirmedNumber(Orders.CONFIRMED));
        orderStatisticsVO.setDeliveryInProgress(adminOrderMapper.getDeliveryInProgressNumber(Orders.DELIVERY_IN_PROGRESS));
        orderStatisticsVO.setToBeConfirmed(adminOrderMapper.getToBeConfirmedNumber(Orders.TO_BE_CONFIRMED));

        return orderStatisticsVO;
    }

    @Override
    public String cancel(Orders orders) {
        orders.setStatus(Orders.CANCELLED);
        orders.setPayStatus(Orders.REFUND);
        orders.setCancelTime(LocalDateTime.now());

        userOrderMapper.update(orders);
        return null;
    }


}
